import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:situation/app/theme/gaps.dart';
import 'package:situation/domain/common/server_config.dart';
import 'package:situation/shared/shared.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

import 'controller.dart';

class ServerUrlSettingPage extends StatefulWidget {
  const ServerUrlSettingPage({Key? key}) : super(key: key);

  @override
  State<ServerUrlSettingPage> createState() => _ServerUrlSettingPageState();
}

class _ServerUrlSettingPageState extends State<ServerUrlSettingPage> {
  final controller = Get.find<ServerUrlSettingController>();
  final state = Get.find<ServerUrlSettingController>().state;

  _buildItem(i) {
    var e = state.serverList[i];
    var isActive = i == UtilSp.getInt(AppConstant.systemConfigIndex);
    return Container(
      margin: const EdgeInsets.all(10),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(10),
        boxShadow: [
          BoxShadow(
            color: Colors.grey.withOpacity(0.5),
            spreadRadius: 1,
            blurRadius: 10,
            offset: const Offset(0, 3), // changes position of shadow
          ),
        ],
      ),
      padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 24),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Row(
            children: [
              GestureDetector(
                onTap: () {
                  controller.select(i);
                },
                child: Container(
                  width: 30,
                  height: 30,
                  decoration: BoxDecoration(
                    color: isActive ? Colors.blue : Colors.grey.withAlpha(80),
                    borderRadius: BorderRadius.circular(30),
                  ),
                  child: Visibility(
                    visible: isActive,
                    child: Icon(Icons.offline_pin_rounded,color: Colors.white, size: 20),
                  ),
                ),
              ),
              Gaps.hGap24,
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    "${e.name}",
                    style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
                  ),
                  Gaps.vGap12,
                  Text(
                    "JAVA服务地址： ${e.serverUrl}",
                    style: TextStyle(fontSize: 12, color: Colors.grey),
                  ),
                  Text(
                    "公网时代通信地址： ${e.intercomUrl}",
                    style: TextStyle(fontSize: 12, color: Colors.grey),
                  ),
                ],
              ),
            ],
          ),

          i == 0
              ? SizedBox()
              : Row(
            children: [
              GestureDetector(
                onTap: () {
                  controller.edit(i);
                },
                child: Icon(Icons.edit),
              ),
              Gaps.hGap24,
              GestureDetector(
                onTap: () {
                  controller.remove(i);
                },
                child: Icon(Icons.restore_from_trash),
              )
            ],
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: TDNavBar(
        title: '服务地址设置',
        rightBarItems: [
          TDNavBarItem(iconWidget: Icon(Icons.add), action: controller.add),
        ],
      ),
      body: Obx(
        () => SingleChildScrollView(
          child: Column(
            children: [
              for (var i = 0; i < state.serverList.length; i++) _buildItem(i),
            ],
          ),
        ),
      ),
    );
  }

  @override
  void dispose() {
    Get.delete<ServerUrlSettingController>();
    super.dispose();
  }
}
